package com.zyd.blog.personal.service.impl;
import	java.util.HashMap;

import com.zyd.blog.persistence.mapper.ProtocolMapper;
import com.zyd.blog.personal.service.ProtocolService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 协议业务逻辑层
 * @author mfy
 */
@Service
public class ProtocolServiceImpl implements ProtocolService {

    @Resource
    private ProtocolMapper protocolMapper;

    @Override
    public boolean savePaotocol(Map<String,Object> map){
        String type = (String) map.get("type");
        if(type == null || "".equals(type)){
            return false;
        }
        String content = (String) map.get("content");
        if(content == null || "".equals(content)){
            return false;
        }
        List<Map<String,Object>> protocols = protocolMapper.queryProtocolByCondition(map);
        map.put("updateTime",(new Date()).getTime());
        if(protocols == null || protocols.size() == 0){
            String title = (String) map.get("title");
            if(title == null || "".equals(title)){
                return false;
            }
            map.put("addTime",(new Date()).getTime());
            return protocolMapper.addProtocolInfo(map) > 0;
        }
        return protocolMapper.updateProtocolInfo(map) > 0;
    }

    private Map<String,Object> getProtocolInfo(String type){
        if(type == null || "".equals(type)){
            return new HashMap<>(0);
        }
        Map<String,Object> conditions = new HashMap<>(16);
        conditions.put("type",type);
        List<Map<String,Object>> protocols = protocolMapper.queryProtocolByCondition(conditions);
        if(protocols == null || protocols.size() == 0){
            return new HashMap<>(0);
        }
        return protocols.get(0);
    }

    @Override
    public String getProtocol(String type){
        Map<String,Object> protocolInfo = getProtocolInfo(type);
        return (String) protocolInfo.get("content");
    }
}
